package com.videoaggre.zcl.helper.player.cover

import android.content.Context
import android.os.Bundle
import android.view.View
import com.kk.taurus.playerbase.event.OnPlayerEventListener
import com.kk.taurus.playerbase.receiver.BaseCover
import com.videoaggre.zcl.R
import com.videoaggre.zcl.common.bindViewClick
import com.videoaggre.zcl.helper.player.DataInter
import com.videoaggre.zcl.ui.widget.CommonButton

/**
 * Created by Taurus on 2018/4/20.
 */
class CompleteCover(context: Context?) : BaseCover(context) {
    private val mRetry: CommonButton by lazy { view.findViewById(R.id.tvRetry) }
    override fun onReceiverBind() {
        bindViewClick(::onClick, mRetry)
    }

    override fun onCoverAttachedToWindow() {
        super.onCoverAttachedToWindow()
        if (groupValue.getBoolean(DataInter.Key.KEY_COMPLETE_SHOW)) {
            setPlayCompleteState(true)
        }
    }

    override fun onCoverDetachedToWindow() {
        super.onCoverDetachedToWindow()
        setCoverVisibility(View.GONE)
    }

    override fun onReceiverUnBind() {
        super.onReceiverUnBind()
    }

    private fun onClick(view: View) {
        when (view) {
            mRetry -> requestReplay(null)
        }
        setPlayCompleteState(false)
    }

    private fun setPlayCompleteState(state: Boolean) {
        setCoverVisibility(if (state) View.VISIBLE else View.GONE)
        groupValue.putBoolean(DataInter.Key.KEY_COMPLETE_SHOW, state)
    }

    override fun onPlayerEvent(eventCode: Int, bundle: Bundle?) {
        when (eventCode) {
            OnPlayerEventListener.PLAYER_EVENT_ON_DATA_SOURCE_SET,
            OnPlayerEventListener.PLAYER_EVENT_ON_VIDEO_RENDER_START -> setPlayCompleteState(false)
            OnPlayerEventListener.PLAYER_EVENT_ON_PLAY_COMPLETE -> setPlayCompleteState(true)
        }
    }

    override fun onErrorEvent(eventCode: Int, bundle: Bundle?) {}
    override fun onReceiverEvent(eventCode: Int, bundle: Bundle?) {}
    public override fun onCreateCoverView(context: Context): View {
        return View.inflate(context, R.layout.view_cover_complete, null)
    }

    override fun getCoverLevel(): Int {
        return levelMedium(20)
    }
}